package com.cakebackoffice.Model;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.cakebackoffice.database.DataSource;
import com.cakebackoffice.database.IDatabasable;
import com.cakebackoffice.database.ProductTable;
import com.cakebackoffice.database.UnitarySaleTable;

import java.util.ArrayList;
import java.util.List;

public class UnitarySale implements IDatabasable {
    private Long idDB;
    private Long remoteID;
    private Float price;
    private Float VAT;
    private ProductType productType = null;
    private Product product = null;
    private MenuType menuType = null;
    private Menu menu = null;

    private UnitarySale(DataSource dataSource, Cursor cursor) {
        idDB = cursor.getLong(cursor.getColumnIndexOrThrow(UnitarySaleTable.ID));
        setRemoteID(cursor.getLong(cursor.getColumnIndexOrThrow(ProductTable.REMOTE_ID)));
        price = cursor.getFloat(cursor.getColumnIndexOrThrow(UnitarySaleTable.PRICE));
        VAT = cursor.getFloat(cursor.getColumnIndexOrThrow(UnitarySaleTable.VAT));
        productType = ProductType.getOne(dataSource, cursor.getInt(cursor.getColumnIndexOrThrow(UnitarySaleTable.PRODUCTTYPEID)));
        product = Product.getOne(dataSource, cursor.getInt(cursor.getColumnIndexOrThrow(UnitarySaleTable.PRODUCTID)));
        menuType = MenuType.getOne(dataSource, cursor.getInt(cursor.getColumnIndexOrThrow(UnitarySaleTable.MENUTYPEID)));
        menu = Menu.getOne(dataSource, cursor.getInt(cursor.getColumnIndexOrThrow(UnitarySaleTable.MENUID)));
    }

    public UnitarySale(Float price, Float VAT, ProductType productType, Product product) {
        this.price = price;
        this.VAT = VAT;
        this.productType = productType;
        if (productType == null && product != null) {
            this.productType = product.getType();
        }
        this.product = product;
    }

    public UnitarySale(Float price, Float VAT, MenuType menuType, Menu menu) {
        this.price = price;
        this.VAT = VAT;
        this.menuType = menuType;
        if (menuType == null && menu != null) {
            menuType = this.menu.getType();
        }
        this.menu = menu;
    }

    public Long getRemoteID() {
        return remoteID;
    }

    private void setRemoteID(Long remoteID) {
        this.remoteID = remoteID;
    }

    public boolean isProduct() throws Exception {
        if (this.getProduct() != null) {
            if (this.getProductType() == null) {
                this.setProductType(this.getProduct().getType());
            }
            if (this.getMenu() != null || this.getMenuType() != null) {
                throw new Exception("Arrrrgh, je sais pas si je suis un menu ou un produit :/");
            }
            return true;
        } else if (this.getProductType() != null) {
            return true;

        } else {
            if (this.getMenu() != null || this.getMenuType() != null) {
                throw new Exception("Arrrrgh, je sais pas si je suis un menu ou un produit :/");
            } else if (this.getMenu() != null && this.getMenuType() == null) {
                this.setMenuType(this.getMenu().getType());
            }
            return false;
        }
    }

    public boolean isMenu() throws Exception {
        return !this.isProduct();
    }


    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public Float getVAT() {
        return VAT;
    }

    public void setVAT(Float VAT) {
        this.VAT = VAT;
    }

    public ProductType getProductType() {
        return productType;
    }

    public void setProductType(ProductType productType) {
        this.productType = productType;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public MenuType getMenuType() {
        return menuType;
    }

    public void setMenuType(MenuType menuType) {
        this.menuType = menuType;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }

    @Override
    public ContentValues getValues() {
        ContentValues values = new ContentValues();
        values.put(UnitarySaleTable.REMOTE_ID, this.getRemoteID());
        values.put(UnitarySaleTable.PRICE, this.getPrice());
        values.put(UnitarySaleTable.VAT, this.getVAT());
        values.put(UnitarySaleTable.PRODUCTTYPEID, this.getProductType() != null ? this.getProductType().getIdDB() : null);
        values.put(UnitarySaleTable.PRODUCTID, this.getProduct() != null ? this.getProduct().getIdDB() : null);
        values.put(UnitarySaleTable.MENUTYPEID, this.getMenuType() != null ? this.getMenuType().getIdDB() : null);
        values.put(UnitarySaleTable.MENUID, this.getMenu() != null ? this.getMenu().getIdDB() : null);

        return values;
    }

    @Override
    public void setIdDB(Long id) {
        this.idDB = id;
    }

    @Override
    public Long getIdDB() {
        return this.idDB;
    }

    @Override
    public void saveToDatabase(Context context) {
        //Protection anti-doublons
        UnitarySale bis = null;
        if (this.getIdDB() == null && this.getRemoteID() != null && (bis = UnitarySale.getOneFromRemoteID(context, this.getRemoteID())) != null) {
            this.setIdDB(bis.getIdDB());
        }

        {
            if (this.getMenu() != null) {
                this.getMenu().saveToDatabase(context);
            }
            if (this.getMenuType() != null) {
                this.getMenuType().saveToDatabase(context);
            }
            if (this.getProduct() != null) {
                this.getProduct().saveToDatabase(context);
            }
            if (this.getProductType() != null) {
                this.getProductType().saveToDatabase(context);
            }
        }

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        if (this.getIdDB() == null) {
            dataSource.insert(this, UnitarySaleTable.getInstance());
        } else {
            dataSource.update(this, UnitarySaleTable.getInstance());
        }

        dataSource.close();
    }

    @Override
    public void removeFromDatabase(Context context) {
        if (this.getIdDB() != null) {
            DataSource dataSource = new DataSource(context);
            dataSource.open();
            dataSource.delete(this, UnitarySaleTable.getInstance());
            dataSource.close();
        }
    }

    public static UnitarySale getOne(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOne(idDB, UnitarySaleTable.getInstance());
        cursor.moveToFirst();
        UnitarySale item = null;
        while (!cursor.isAfterLast()) {
            item = new UnitarySale(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static UnitarySale getOneFromRemoteID(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOneFromRemoteID(idDB, UnitarySaleTable.getInstance());
        cursor.moveToFirst();
        UnitarySale item = null;
        while (!cursor.isAfterLast()) {
            item = new UnitarySale(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }


    public static List<UnitarySale> getAll(Context context) {
        List<UnitarySale> result = new ArrayList<UnitarySale>();
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchAll(UnitarySaleTable.getInstance());
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            result.add(new UnitarySale(dataSource, cursor));
            cursor.moveToNext();
        }
        dataSource.close();
        return result;
    }

    public static UnitarySale getOne(DataSource dataSource, long idDB) {
        Cursor cursor = dataSource.fetchOne(idDB, UnitarySaleTable.getInstance());
        cursor.moveToFirst();
        UnitarySale item = null;
        while (!cursor.isAfterLast()) {
            item = new UnitarySale(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        return item;
    }
}
